import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'controller/index_controller.dart';
import 'net/dio_utils.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2020/7/20.
///
/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2020/9/25.
///
/// gongzhonghao biglead
/// gongzhonghao biglead
/// github https://github.com/zhaolongs
/// bili https://space.bilibili.com/513480210
/// zhihu https://www.zhihu.com/people/zhao-long-90-89
/// csdn https://blog.csdn.net/zl18603543572
/// 西瓜视频 https://www.ixigua.com/home/3662978423///lib/src/index_page.dart
///
///代码清单12-3 启动页面
class IndexPage extends StatelessWidget {
  const IndexPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    //依赖注入
    Get.put(IndexController(), permanent: true);
    return Scaffold(
      ///层叠布局
      body: SizedBox(
        width: double.infinity,
        height: double.infinity,
        child: Stack(
          alignment: Alignment.center,
          children: [
            ///构建背景
            Positioned(
                child: Image.asset(
                  "assets/images/welcome_bg.jpeg",
                  fit: BoxFit.fill,
                ),
                left: 0,
                right: 0,
                top: 0,
                bottom: 0),
            Positioned.fill(
              child: Container(
                color: const Color(0x50ffffff),
              ),
            ),
            GetBuilder(
              builder: (IndexController controller) {
                return buildLoading(controller);
              },
            ),
          ],
        ),
      ),
    );
  }

  buildLoading(IndexController controller) {
    if (controller.loadingStatues == LoadingStatues.loading) {
      //显示一个加载小圆圈
      return const CupertinoActivityIndicator();
    }
    //加载失败 显示原因 点击重新发起加载
    if (controller.loadingStatues == LoadingStatues.faile) {
      return TextButton(
        onPressed: () {
          controller.loadingStatues = LoadingStatues.loading;
          controller.initData(update: true);
        },
        child: Text(controller.errMsg),
      );
    }
  }
}
